package paylayer

import (
    "fmt"
	"strconv"
	"strings"
	"dhfshop/models/misc"
	"encoding/json"
	"dhfshop/models/isv"
	"dhfshop/models/shopping"
	"github.com/astaxie/beego"
	"thirdpaysdk/api/gpslmall"
	"dhfshop/service/redisgo"
	"github.com/pkg/errors"
)


func OrderGpslmallPay(appId,openId string,acPay *isv.AcPayParams,order *shopping.ValueOrder)(rsp PayResponse,prepayId string, err error) {
	
	shopKey := acPay.SubMchId
	secrecode := acPay.Appkey
	orderStr := strconv.FormatInt(order.OrderId,10)
	payment := strconv.FormatFloat(float64(order.Payment), 'f', -1, 32)
	
	productName := "order" + "[" + orderStr + "]"
	payNotify := beego.AppConfig.String("WXPayNotify") + "/public/gpslmall/notify"
	gpslpayRsp, err := gpslmall.MicroAppPay(shopKey,secrecode,appId,orderStr,payment,openId,payNotify,productName)
	if err != nil {
		beego.Debug(fmt.Sprintf("AppId:%s MicroAppPay error:%s",appId, err.Error()))
		return 
	}
	//beego.Debug(fmt.Sprintf("AppId:%s MicroAppPay Package:%s",appId, gpslpayRsp.Package))
	prepayId = strings.Replace(gpslpayRsp.Package, "prepay_id=", "", -1)
	
	//更新订单支付方式编号
	order.PayWayCode = 22
	shopping.UpdateOrderPayWayCode(order)
	payRsp := misc.WxResponse{}
	payRsp.AppId = gpslpayRsp.AppId
	payRsp.NonceStr = gpslpayRsp.NonceStr
	payRsp.Package = gpslpayRsp.Package
	payRsp.SignType = gpslpayRsp.SignType
	payRsp.TimeStamp = gpslpayRsp.TimeStamp
	payRsp.PaySign = gpslpayRsp.PaySign
	if tempJson, err := json.Marshal(payRsp); err == nil { 
	   rsp.WxpayRsp = string(tempJson)
	}
	redisconn := redisgo.GetInstance()
	cacheKey := "payorder_" + orderStr
	var payInfoCache *PayInfoCache = new(PayInfoCache)
	payInfoCache.AppId = appId
	payInfoCache.MchId = acPay.MchId
	payInfoCache.AppKey = acPay.Appkey
	payInfoCache.OrderKind = "ESHOP_ORDER_PAY"
	if payInfoJSON, err := json.Marshal(payInfoCache); err == nil { 
		if _, err = redisconn.Set(cacheKey, string(payInfoJSON), 72*60*60); err != nil {  
			beego.Debug("SETEX", err.Error())
		}
	}
	return
}

func ChargeGpslmallPay(appId,openId,outTradeNo string,acPay *isv.AcPayParams,amount float32)(rsp PayResponse,prepayId string,err error){
    shopKey := acPay.SubMchId
	secrecode := acPay.Appkey
	productName := "charge" + "[" + outTradeNo + "]"
	payment := strconv.FormatFloat(float64(amount), 'f', -1, 32)
	payNotify := beego.AppConfig.String("WXPayNotify") + "/public/royalpay/notify"
	gpslpayRsp, err := gpslmall.MicroAppPay(shopKey,secrecode,appId,outTradeNo,payment,openId,payNotify,productName)
	if err != nil {
		err = errors.Wrap(err, "MicroAppPay... ")
		beego.Debug(err.Error())
		return 
	}
	prepayId = strings.Replace(gpslpayRsp.Package, "prepay_id=", "", -1)
	payRsp := misc.WxResponse{}
	payRsp.AppId = gpslpayRsp.AppId
	payRsp.NonceStr = gpslpayRsp.NonceStr
	payRsp.Package = gpslpayRsp.Package
	payRsp.SignType = gpslpayRsp.SignType
	payRsp.TimeStamp = gpslpayRsp.TimeStamp
	payRsp.PaySign = gpslpayRsp.PaySign
	if tempJson, err := json.Marshal(payRsp); err == nil {
		rsp.WxpayRsp = string(tempJson)
	}
	redisconn := redisgo.GetInstance()
	cacheKey := "payorder_" + outTradeNo
	var payInfoCache *PayInfoCache = new(PayInfoCache)
	payInfoCache.AppId = appId
	payInfoCache.MchId = acPay.MchId
	payInfoCache.AppKey = acPay.Appkey
	payInfoCache.OrderKind = "MEMBER_CHARGE"
	if payInfoJSON, err := json.Marshal(payInfoCache); err == nil { 
		if _, err = redisconn.Set(cacheKey, string(payInfoJSON), 72*60*60); err != nil {  
			beego.Debug("SETEX", err.Error())
		}
	}
	return
}

func CommonGpslmallPay(appId,openId,orderId,body,kind string,acPay *isv.AcPayParams,amount float32)(rsp PayResponse,prepayId string, err error){
    shopKey := acPay.SubMchId
	secrecode := acPay.Appkey
	payment := strconv.FormatFloat(float64(amount), 'f', -1, 32)
	payNotify := beego.AppConfig.String("WXPayNotify") + "/public/royalpay/notify"
	gpslpayRsp, err := gpslmall.MicroAppPay(shopKey,secrecode,appId,orderId,payment,openId,payNotify,body)
	if err != nil {
		err = errors.Wrap(err, "MicroAppPay... ")
		beego.Debug(err.Error())
		return 
	}
	prepayId = strings.Replace(gpslpayRsp.Package, "prepay_id=", "", -1)
	payRsp := misc.WxResponse{}
	payRsp.AppId = gpslpayRsp.AppId
	payRsp.NonceStr = gpslpayRsp.NonceStr
	payRsp.Package = gpslpayRsp.Package
	payRsp.SignType = gpslpayRsp.SignType
	payRsp.TimeStamp = gpslpayRsp.TimeStamp
	payRsp.PaySign = gpslpayRsp.PaySign
	if tempJson, err := json.Marshal(payRsp); err == nil { 
	   rsp.WxpayRsp = string(tempJson)
	}
	redisconn := redisgo.GetInstance()
	cacheKey := "payorder_" + orderId
	var payInfoCache *PayInfoCache = new(PayInfoCache)
	payInfoCache.AppId = appId
	payInfoCache.MchId = acPay.MchId
	payInfoCache.AppKey = acPay.Appkey
	payInfoCache.OrderKind = kind
	if payInfoJSON, err := json.Marshal(payInfoCache); err == nil { 
		if _, err = redisconn.Set(cacheKey, string(payInfoJSON), 72*60*60); err != nil {  
			beego.Debug("SETEX", err.Error())
		}
	}
	return
}
